#include "croot.h"
#include "cpixmapdecoration.h"

#include <buola/gui/x11/cx11display.h>
#include <buola/gui/x11/cpixmap.h>
#include <buola/gl.h>
#include "cclient.h"

#include <X11/Xregion.h>

namespace buola { namespace bwm {

CPixmapDecoration::CPixmapDecoration(CClient *pClient)
    :   CDecoration(pClient)
{
    mPixmap=nullptr;
    mPixmapValid=false;
    mTexture=new gl::CTexture;
}

CPixmapDecoration::~CPixmapDecoration()
{
//    if(mTexture->IsBound())
//        mTexture->Unbind();
    
    if(mPixmap)
    {
        delete mPixmap;
        mPixmap=nullptr;
    }
    
//    delete mTexture;
}

void CPixmapDecoration::Paint(const CRegion &pRegion,int pDesktop)
{
/*    if(!mPixmap||!mPixmapValid)
    {
        if(!mPixmap&&mTexture->IsBound())
            mTexture->Unbind();
        CreatePixmap();
    }
    if(!mTexture->IsBound())
        mTexture->Bind(mPixmap);

    CGLMatrix lMatrix=mTexture->mMatrix;
    lMatrix.x0-=mClient->GetWholeRect(pDesktop).l*lMatrix.xx;
    lMatrix.y0-=mClient->GetWholeRect(pDesktop).t*lMatrix.yy;

    mTexture->Enable(false);

    Region lR=pRegion.GetID();
    
    int lVSize=24*lR->numRects;
    GLfloat lVertices[lVSize];

    GLfloat *lV=lVertices;
    
    for(int i=0;i<lR->numRects;i++)
    {
        BoxPtr lB=&lR->rects[i];

        *(lV++)=lB->x1*lMatrix.xx+lMatrix.x0;
        *(lV++)=lB->y2*lMatrix.yy+lMatrix.y0;

        *(lV++)=lB->x1;
        *(lV++)=lB->y2;
        *(lV++)=0;

        *(lV++)=lB->x2*lMatrix.xx+lMatrix.x0;
        *(lV++)=lB->y2*lMatrix.yy+lMatrix.y0;

        *(lV++)=lB->x2;
        *(lV++)=lB->y2;
        *(lV++)=0;

        *(lV++)=lB->x2*lMatrix.xx+lMatrix.x0;
        *(lV++)=lB->y1*lMatrix.yy+lMatrix.y0;

        *(lV++)=lB->x2;
        *(lV++)=lB->y1;
        *(lV++)=0;

        *(lV++)=lB->x1*lMatrix.xx+lMatrix.x0;
        *(lV++)=lB->y1*lMatrix.yy+lMatrix.y0;

        *(lV++)=lB->x1;
        *(lV++)=lB->y1;
        *(lV++)=0;
    }

    glEnableVertexAttribArray(3);
    glVertexAttribPointer(3,2,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),lVertices);
    glVertexAttribPointer(0,3,GL_FLOAT,GL_FALSE,5*sizeof(GLfloat),lVertices+2);

    glDrawArrays(GL_QUADS,0,4*lR->numRects);

    mTexture->Disable();
*/
}

void CPixmapDecoration::PaintBaseLine()
{
/*
    if(!mPixmap||!mPixmapValid)
        CreatePixmap();
    if(!mTexture->IsBound())
        mTexture->Bind(mPixmap);

    CPoint_d lPoint(-mClientSize.x/2+GetTotalExtent().l,
                    -mClientSize.y+GetTotalExtent().t);

    CGLMatrix lMatrix=mTexture->mMatrix;
    lMatrix.x0-=lPoint.x*lMatrix.xx;
    lMatrix.y0-=lPoint.y*lMatrix.yy;

    mTexture->Enable(false);
    CRect_d lDecoRect=CRect(lPoint,mClientSize+GetTotalExtent().Size());
    CGL::DrawRect(lDecoRect,lMatrix);
    mTexture->Disable();
*/
}

void CPixmapDecoration::Refresh()
{
    mPixmapValid=false;
    CDecoration::Refresh();
}

void CPixmapDecoration::OnGeometryChanged(bool pResize,const CPoint_i &pPos,
                            const CSize_i &pSize,const CPoint_i &pXPos)
{
    CDecoration::OnGeometryChanged(pResize,pPos,pSize,pXPos);

    if(pResize)
    {
//        if(mTexture->IsBound())
//            mTexture->Unbind();
    
//        if(mPixmap)
//        {
//            delete mPixmap;
//            mPixmap=nullptr;
//        }
    }
}

/*namespace bwm*/ } /*namespace buola*/ }
